/*
 * =====================================================================================
 *
 *       Filename:  output.cpp
 *
 *    Description:  result output
 *
 *        Version:  1.0
 *        Created:  08/10/2009 03:25:25 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Zhao Zhao 
 *        Company:  ndssl
 *
 * =====================================================================================
 */

#include "output.h"
using namespace std;
/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  PrintRawDataFile
 *  Description:  output raw data to file "graph_measure.data"
 * =====================================================================================
 */
void PrintRawDataFile(string gTitle, string measure, MeasureVec val, VSize size, Index index)
{
	gTitle = gTitle + "_" + measure;
	gTitle.append(".data");
	cout<<"writing raw data to "<<gTitle<<endl;
	ofstream ofs(gTitle.c_str());
	if(!ofs.is_open())
	{
		cout<<"failed to open raw data file for writing"<<endl;
		exit(1);
	}
	for(VSize i = 0; i<size; i++)
	{
		ofs<<index[i]<<"\t"<<val[i]<<endl;
	}
	ofs.close();
	cout<<"raw data writing success"<<endl<<endl;
}
/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  GenPlotScript
 *  Description:  generate plot script for gnuplot, and plot eps file 
 *  			  output file will be "graph_measure.plt" and "graph_measure.eps"
 * =====================================================================================
 */
void GenPlotScript(string gTitle, string measure)
{
	cout<<"generating plot script...";
	gTitle = gTitle + "_" + measure; 
	string epsFile = gTitle;
	string pltFile = gTitle;
	string distFile = gTitle;
	pltFile.append(".plt");
	epsFile.append(".eps");
	distFile.append(".dist");
	ofstream ofs(pltFile.c_str());
	if(!ofs.is_open())
	{
		cout<<"failed to open plot file for writing"<<endl;
		exit(1);
	}
	ofs<<"set terminal postscript eps enhanced color \"Times-Roman\" 25"<<endl;
	ofs<<"set output "<<"'"<<epsFile<<"'"<<endl;
	ofs<<"set log y"<<endl;
	ofs<<"set size 1,1"<<endl;
	ofs<<"set xlabel \""<<measure<<" Value\""<<endl;
	ofs<<"set ylabel \"# of Nodes\""<<endl;
	ofs<<"set key top right"<<endl;
	ofs<<"set xrange [0:1]"<<endl;
	ofs<<"plot '"<<distFile<<"' using ($1):($2) title "<<"\""<<gTitle<<"\" "<<"w l"<<endl;     
	ofs<<"replot"<<endl; 
	ofs.close();
	cout<<"Plotting distribution into "<<gTitle<<".eps"<<endl;
	system("gnuplot *.plt");
	cout<<"plotting success"<<endl<<endl;
}

/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  CalDist
 *  Description:  calculate histogram of an array of values, 
 *                and output to "graph_measure.dist" file
 * =====================================================================================
 */
void CalDist(MeasureVec valVec, VSize size, float step, string gTitle, string measure)
{
	gTitle = gTitle + "_" + measure;
	gTitle.append(".dist");
	cout<<"calculating measurement distribution and write to "<<gTitle<<endl;
	ofstream ofs(gTitle.c_str());	
	if(!ofs.is_open())
	{
		cout<<"failed to open distribution file for writing"<<endl;
		exit(1);
	}
	float maxTmp;
	int posTmp;
	GetMax(valVec, size, maxTmp, posTmp);
	int bin = maxTmp/step+1;
	int* dist = new int [bin];
	cout<<bin<<" buckets will be selected"<<endl;	
	for(int i = 0; i<bin; i++)
	{
		dist[i] = 0;
	}
	for(VSize i = 0; i<size; i++)
	{
		int pos = valVec[i]/step;
		dist[pos]++;
	}
	for(int i = 0; i<bin; i++)
	{
		ofs<<(step*i)<<"\t"<<dist[i]<<endl;
	}
	ofs.close();
	cout<<"distribution calculation success"<<endl<<endl;
	
}

/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  PrintStatFile
 *  Description:  print statistic data to "graph_measure.stat", 
 *  			  including maxN max values, minN min values, mean value, etc.
 * =====================================================================================
 */

void PrintStatFile(string gTitle, string measure, const MeasureVec valVec, const VSize gSize, Index index, const int maxN, const int minN)
{

	gTitle = gTitle + "_" + measure;
	gTitle.append(".stat");
	cout<<"output "<<maxN<<" largest values, "<<minN<<" smallest values and mean value to "<<gTitle<<endl; 
	ofstream ofs(gTitle.c_str());	
	if(!ofs.is_open())
	{
		cout<<"failed to open statistic file for writing"<<endl;
		exit(1);
	}
	ofs<<"max value"<<endl;
	
	MeasureVec maxVec = new MeasureVal [maxN];
	VSize* maxPos = new VSize [maxN];
	GetMaxN(valVec, gSize, maxVec, maxPos, maxN);

	for(VSize i = 0; i<maxN; i++)
	{
		ofs<<index[maxPos[i]]<<"\t"<<maxVec[i]<<endl;
	}	
	ofs.close();
	cout<<"statistics calculation success"<<endl<<endl;
}	
